using System;
using System.Web.Security;
using System.Xml.Serialization;

namespace Jaws.Core.Web.Entities.Users
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    [XmlDataDirectory("users")]
    public class User : XmlBusinessBase<User>
    {
        public User()
        {
            Key = Guid.NewGuid();
            IsLocked = false;
        }

        /// <summary>
        /// Gets or sets the key.
        /// </summary>
        /// <value>The key.</value>
        [XmlAttribute("id")]
        public Guid Key { get; set; }

        /// <summary>
        /// Gets or sets the name of the user.
        /// </summary>
        /// <value>The name of the user.</value>
        [XmlAttribute("username")]
        public string UserName { get; set; }

        /// <summary>
        /// Gets or sets the email.
        /// </summary>
        /// <value>The email.</value>
        [XmlAttribute("email")]
        public string Email { get; set; }

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        [XmlElement("description")]
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        /// <value>The password.</value>
        [XmlElement(typeof (byte[]), DataType = "base64Binary", ElementName = "password")]
        public byte[] Password { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is locked.
        /// </summary>
        /// <value><c>true</c> if this instance is locked; otherwise, <c>false</c>.</value>
        [XmlAttribute("locked")]
        public bool IsLocked { get; set; }

        /// <summary>
        /// Gets or sets the challenge question.
        /// </summary>
        /// <value>The challenge question.</value>
        [XmlElement("question")]
        public string ChallengeQuestion { get; set; }

        /// <summary>
        /// Gets or sets the challenge answer.
        /// </summary>
        /// <value>The challenge answer.</value>
        [XmlElement("answer")]
        public string ChallengeAnswer { get; set; }

        /// <summary>
        /// Gets or sets the last logged in.
        /// </summary>
        /// <value>The last logged in.</value>
        [XmlAttribute("lastLogin")]
        public DateTime LastLoggedIn { get; set; }

        /// <summary>
        /// Gets or sets the last activity.
        /// </summary>
        /// <value>The last activity.</value>
        [XmlAttribute("lastActivity")]
        public DateTime LastActivity { get; set; }

        /// <summary>
        /// Gets or sets the last password change.
        /// </summary>
        /// <value>The last password change.</value>
        [XmlAttribute("lastPasswordChange")]
        public DateTime LastPasswordChange { get; set; }

        /// <summary>
        /// Gets or sets the last locked out.
        /// </summary>
        /// <value>The last locked out.</value>
        [XmlAttribute("lastLockedOut")]
        public DateTime LastLockedOut { get; set; }

        /// <summary>
        /// Gets or sets the created on.
        /// </summary>
        /// <value>The created on.</value>
        [XmlAttribute("createdOn")]
        public DateTime CreatedOn { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is active.
        /// </summary>
        /// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value>
        [XmlAttribute("active")]
        public bool IsActive { get; set; }

        internal MembershipUser CreateMembershipUser(string providerName)
        {
            return new MembershipUser(providerName, UserName, Key, Email, ChallengeQuestion,
                                      Description, IsActive, IsLocked, CreatedOn, LastLoggedIn, LastActivity,
                                      LastPasswordChange, LastLockedOut);
        }
    }
}